//
//  DYYuyinJiangjieCell.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/4.
//  Copyright © 2021 叁拾叁. All rights reserved.
//


import UIKit

class DYYuyinJiangjieCell: SYBaseCell , UITableViewDelegate, UITableViewDataSource {
    
    var model: DYJingquDetailModel!
    var tableView: UITableView!

    var bgView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = UIColor(hex: 0x367FF6)
        v.layer.cornerRadius = 2
        return v
    }()
    
    var sepView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = Constant.bgViewColor
        return v
    }()
    
    let nameLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x1E1E1E)
        v.font = 17.autoBoldFontSize()
        v.text = "语音讲解"
        return v
    }()
    
    let chakan: UIButton = {
        let v: UIButton  = UIButton()
        v.setTitle("更多 >", for: .normal)
        v.setTitleColor(UIColor(hex: 0x5B5B5B), for: .normal)
        v.titleLabel?.font = 14.autoFontSize()
        v.layer.cornerRadius = 13.5
        v.backgroundColor = UIColor.white
        return v
    }()

    public func configureView(_ model: DYJingquDetailModel) {
        self.model = model
//        if let c = model.tourScenicExplainVO?.count, c < 2 {
//            chakan.isHidden = true
//        }
        self.tableView.reloadData()
    }
    
    override func initViews() {
        chakan.addTarget(self, action: #selector(chakanAction), for: .touchUpInside)
        setTableView()
        contentView.addSubview(nameLb)
        contentView.addSubview(bgView)
        contentView.addSubview(chakan)
        contentView.addSubview(tableView)
        contentView.addSubview(chakan)
        contentView.addSubview(sepView)
        bgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(16.0.autoWidth())
            make.left.equalToSuperview().offset(autoWidth(14.5))
            make.width.equalTo(4.0.autoWidth())
            make.height.equalTo(15.0.autoWidth())
        }
        nameLb.snp.makeConstraints { (make) in
            make.centerY.equalTo(bgView)
            make.left.equalTo(bgView.snp.right).offset(autoWidth(7.0))
            make.right.equalToSuperview().offset(autoWidth(-15.0))
            make.height.equalTo(16.0.autoWidth())
        }
        tableView.snp.makeConstraints { (make) in
            make.top.equalTo(bgView.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(112.0.autoWidth())
        }
        chakan.snp.makeConstraints { (make) in
            make.centerY.equalTo(nameLb).offset(3.0.autoWidth())
            make.right.equalToSuperview().offset(autoWidth(-15.0))
            make.height.equalTo(30.0.autoWidth())
        }
        sepView.snp.makeConstraints { (make) in
            make.top.equalTo(tableView.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(10.0.autoWidth())
            make.bottom.equalToSuperview()
        }
    }
    
    func setTableView() {
        
        //创建表格视图
        self.tableView = UITableView(frame: CGRect(x: 0, y: NAV_HEIGHT, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-NAV_HEIGHT-BOTTOM_SAFE_HEIGHT), style:.plain)
        self.tableView.separatorStyle = .none
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.isScrollEnabled = false
        self.tableView!.register(DYYuyinJIangjieSubCell.self, forCellReuseIdentifier: "Cell")
        self.contentView.addSubview(self.tableView!)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: DYYuyinJIangjieSubCell? = tableView.dequeueReusableCell(withIdentifier: "Cell") as? DYYuyinJIangjieSubCell
        cell?.selectionStyle = .none
        if let m = model , let t = m.tourScenicExplainVO {
            cell?.configureView(t)
        }
        return cell!
    }
    
    @objc func chakanAction() {
        let vc = DYJiangjieListViewController(id: self.model.id ?? "")
        self.currentVC.navigationController?.pushViewController(vc, animated: true)
    }
}

